Initialize your Service
In this guide, you will create a service called dummy-controller
in Go (Golang), interface it with the official imaging
and actuator
services, upload it to the Rover and execute it.
Elias Groot
Software Lead, Course Organizer
Before you can get started writing any code, make sure to properly initialize your workspace on your own device (your laptop).
- Create a folder called dummy-controller and enter it
mkdir dummy-controller && cd dummy-controller
- Initialize the required folder structure using
roverctl
and enter the required information
roverctl service init
(Alternatively, you can clone our Go service template)
- Upload the initial version of your service to the Rover (if it is powered on)
roverctl service sync
- (Optional) open the directory in VS Code and hit ctrl+shift+p. Then select "Dev Containers: Rebuild and Reopen in Container". The build process might take a while the first time. Once built, open a terminal in VS Code and try to build the service
make build
Service Entrypoint
Take a look at the main()
function in src/main.go. You will see a reference to roverlib.run()
. The defined run()
function is the entrypoint of your service, it will be executed once the roverlib
processed your service data. This is where your code should live.
The provided run()
function already illustrates how to use the most important methods that the roverlib-go
library provides. Try and see if you can follow what the example code does and why you might need this.
After that, clear the contents of the run()
function. We will start from scratch, so your source code should look like this:
package main
import (
"fmt"
"os"
"time"
pb_outputs "github.com/VU-ASE/rovercom/packages/go/outputs"
roverlib "github.com/VU-ASE/roverlib-go/src"
"github.com/rs/zerolog/log"
)
// The main user space program
// this program has all you need from roverlib: service identity, reading, writing and configuration
func run(service roverlib.Service, configuration *roverlib.ServiceConfiguration) error {
}
// This function gets called when roverd wants to terminate the service
func onTerminate(sig os.Signal) error {
log.Info().Str("signal", sig.String()).Msg("Terminating service")
//
// ...
// Any clean up logic here
// ...
//
return nil
}
// This is just a wrapper to run the user program
// it is not recommended to put any other logic here
func main() {
roverlib.Run(run, onTerminate)
}